package database;

import java.util.ArrayList;

import baseclasses.Assignment;
import baseclasses.Course;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.preference.PreferenceManager;
import android.util.Log;

public class AssignmentDAO {

	public static long addAssignment(Context context, Assignment assignment){	//Adds assignment to the database

		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.ASSIGNMENTNAME, assignment.getName());
		values.put(DatabaseHelper.ASSIGNMENTDESCRIPTION, assignment.getDescription());
		values.put(DatabaseHelper.COURSEID, assignment.getCourseId());
		values.put(DatabaseHelper.ASSIGNMENTDUEDATE, assignment.getDueDateLong());
		values.put(DatabaseHelper.ASSIGNMENTCOMPLETED, boolToInt(assignment.isCompleted()));

		long result = db.insert(DatabaseHelper.ASSIGNMENTTABLE, null, values);
		Log.d("ASSIGNMENT","Assignment  with _id ="+assignment.getId()+" added");	//Debugging
		db.close();

		return  result;

	}

	public static ArrayList<Assignment> getAssignmentsWithPreferences( Context context ){	
		//Determine the sorting preference and use that to determine how the assignment arraylist is populated
		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();
		ArrayList<Assignment> assignments = new ArrayList<Assignment>();

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( context );
		String whereClause = null;
		String orderByString = DatabaseHelper.ASSIGNMENTDUEDATE +" ASC";
		String orderByPref = prefs.getString("sortbypref", "Due Date");	
		boolean hideCompleted = prefs.getBoolean("hidecompletepref", false);
		boolean completedLast = prefs.getBoolean("completedlastref", false);
		
		if( orderByPref.equals("Due Date") )
		{
			orderByPref = DatabaseHelper.ASSIGNMENTDUEDATE +" ASC";	//Ascending, so whatever's due first will appear first in the list
		}
		else
		{
			orderByPref = DatabaseHelper.COURSEID +" ASC";	//else sort by course id
		}

		if( hideCompleted )
		{
			whereClause = DatabaseHelper.ASSIGNMENTCOMPLETED + " = 0";	//since 0 is false for marked completed, it'll only get those that aren't complete
		}
		else if( completedLast )
		{
			orderByString = DatabaseHelper.ASSIGNMENTCOMPLETED +" ASC, " + orderByPref;		//if they are completed and are showing, put them last (0's first, 1's last)
		}
		else
		{
			orderByString = orderByPref;
		}


		Cursor cursor = db.query(DatabaseHelper.ASSIGNMENTTABLE, 
				null, whereClause, null, null ,null, orderByString);

		Assignment assignment;
		while(cursor.moveToNext()){
			assignment = new Assignment();
			assignment.setId(cursor.getInt(0));
			assignment.setName(cursor.getString(1));
			assignment.setDescription(cursor.getString(2));
			assignment.setCourseId(cursor.getInt(3));
			assignment.setDueDate(cursor.getLong(4));
			assignment.setCompleted(cursor.getInt(5));
			assignments.add(assignment);
		}

		cursor.close();
		db.close();

		return assignments;
	}

	public static ArrayList<Assignment> getAssignments(Context context, String orderBy){

		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();
		ArrayList<Assignment> assignments = new ArrayList<Assignment>();

		Cursor cursor = db.query(DatabaseHelper.ASSIGNMENTTABLE, 
				null,null,null,null,null,orderBy);

		Assignment assignment;
		while(cursor.moveToNext()){
			assignment = new Assignment();
			assignment.setId(cursor.getInt(0));
			assignment.setName(cursor.getString(1));
			assignment.setDescription(cursor.getString(2));
			assignment.setCourseId(cursor.getInt(3));
			assignment.setDueDate(cursor.getLong(4));
			assignment.setCompleted(cursor.getInt(5));
			assignments.add(assignment);
		}

		cursor.close();
		db.close();

		return assignments;
	}


	public static ArrayList<Assignment> getAssignmentsByCourse(Context context, Course course){
		//Must return assignment arraylist that is populated with items from the db
		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();
		ArrayList<Assignment> assignments = new ArrayList<Assignment>();

		Cursor cursor = db.query(DatabaseHelper.ASSIGNMENTTABLE, 
				null,DatabaseHelper.COURSEID+" = "+course.getId(),null,null,null,null);

		Assignment assignment;
		while(cursor.moveToNext()){
			assignment = new Assignment();
			assignment.setId(cursor.getInt(0));
			assignment.setName(cursor.getString(1));
			assignment.setDescription(cursor.getString(2));
			assignment.setCourseId(cursor.getInt(3));
			assignment.setDueDate(cursor.getLong(4));
			assignment.setCompleted(cursor.getInt(5));
			assignments.add(assignment);
		}

		cursor.close();
		db.close();

		return assignments;
	}


	public static Assignment getAssignmentById(Context context, int id){
		//query based on assignment id passed from context return the assignment object as result
		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();

		Cursor cursor = db.query(DatabaseHelper.ASSIGNMENTTABLE, 
				null,"_id = "+id,null,null,null,null);

		Assignment assignment = null;

		if(cursor.moveToNext()){
			assignment = new Assignment();
			assignment.setId(cursor.getInt(0));
			assignment.setName(cursor.getString(1));
			assignment.setDescription(cursor.getString(2));
			assignment.setCourseId(cursor.getInt(3));
			assignment.setDueDate(cursor.getLong(4));
			assignment.setCompleted(cursor.getInt(5));
		}
		cursor.close();
		db.close();

		return assignment;
	}

	public static int deleteAssignmentsByCourse(Context context, Course course){
		//query for all assignments by course (returned in an arraylist) and then delete them and associated reminders
		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();

		ArrayList<Assignment> assignmentsByCourse = getAssignmentsByCourse(context, course);

		for(int i=0; i < assignmentsByCourse.size(); i++){
			Assignment a = assignmentsByCourse.get(i);
			deleteAssignment(context, a);
			ReminderDAO.deleteReminderByAssignment(context, a);
		}

		db.close();

		return 0;
	}

	public static int editAssignment(Context context, Assignment assignment){

		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();
		//get all new assignment values passed from context (edit assignment activity) and update the info in the db
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.ASSIGNMENTNAME,assignment.getName());
		values.put(DatabaseHelper.ASSIGNMENTDESCRIPTION,assignment.getDescription());
		values.put(DatabaseHelper.ASSIGNMENTDUEDATE,assignment.getDueDateLong());
		values.put(DatabaseHelper.COURSEID, assignment.getCourseId());
		values.put(DatabaseHelper.ASSIGNMENTCOMPLETED, boolToInt(assignment.isCompleted()));

		int result = db.update(DatabaseHelper.ASSIGNMENTTABLE, values, "_id = "+assignment.getId(),null);

		db.close();

		return result;

	}

	public static int deleteAssignment(Context context, Assignment assignment){
		
		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();
		//delete assignment and its reminder
		int result =  db.delete(DatabaseHelper.ASSIGNMENTTABLE, "_id ="+assignment.getId(), null);
		ReminderDAO.deleteReminderByAssignment(context, assignment);
		db.close();

		return result;
	}

	public static int markAssigment(Context context, Assignment assignment)
	{
		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();
		//call booltoInt and use that to determine the mark (0 false, 1 true) for completed or not
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.ASSIGNMENTCOMPLETED,boolToInt( assignment.isCompleted() ) );

		int result = db.update(DatabaseHelper.ASSIGNMENTTABLE, values, "_id = "+assignment.getId(),null);
		db.close();

		return result;
	}


	public static int boolToInt( boolean bool )
	{
		if( bool )
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}

}
